
function reload() {
    var formParam = {
    };
    $.ajax({
        type: 'post',
        url: '/System/restindex/ReloadRestIndex',
        data: formParam,
        cache: false,
        dataType: 'json',
        success: function (data) {
            if (data.success == true) {
                for (var i = 0; i < data.data.length; i++) {
                    var d = data.data[i];
                    setObj(d);
                }
            }
            else {
                //alert(data.message);
            }
        },
        error: function () {
            //alert('e');
        }
    });
}

//重新加载对象
function setObj(data) {
//	alert("json:" + JSON.stringify(data));
    var o = $("#table" + data.tableID);
    if (o == null) {
    	return;
    }
    $(o).removeClass("tablepic0");
    $(o).removeClass("tablepic1");
    $(o).removeClass("tablepic2");
    $(o).removeClass("tablepic3");
    $(o).removeClass("tablepic4");

    $(o).addClass("tablepic"+data.tableStatus);

    $(o).find(".table-status").html(data.tableStatusStr);

    $(o).find(".table-price").html("");
    $(o).find(".table-people").html("");
    $(o).find(".table-addfood").html("");
    if (data.tableStatus == 3 || data.tableStatus == 4)
    {
        $(o).find(".table-price").html("¥"+data.totalPrice + "元");
        $(o).find(".table-people").html("("+data.peopleNum + "人)");
    }
    if (data.table_status == 3 && data.table_status1 > 0) {
        $(o).find(".table-addfood").html("<img src='/images/new.png' alt='' />");
    }
}

function load() {
	$("#spancur").html(curdate());
    reload();

    ///定时执行
    setTimeout(function () {
        load();
    }, 10000)
}

load();


function curdate() {
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    if (month < 10) {
        month = '0' + month;
    }
    var day = date.getDate();
    if (day < 10) {
        day = '0' + day;
    }
    var hour = date.getHours();
    if (hour < 10) {
        hour = '0' + hour;
    }
    var minute = date.getMinutes();
    if (minute < 10) {
        minute = '0' + minute;
    }
    var second = date.getSeconds();
    if (second < 10) {
        second = '0' + second;
    }
    return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
}



function clicktable(divid) {
    $(".actived").html("");
    $(divid).find(".actived").html("<span></span>");
    $("#hidtableid").val($(divid).find(".tableid").val());
    $("#hidtableid").val(divid);
    WinLogin("登录验证", "loginjs");
}

function loginjs(r)
{
    if (r != 1) return;
//    alert("tableid :" + $("#hidtableid").val());
    var formParam = {
            param: $("#hidtableid").val(),
        };
    $.ajax({
        type: 'post',
        url: '/System/restindex/GetPicInfobytableid/',
        data: formParam,
        cache: false,
        dataType: 'json',
        success: function (data) {
//        	alert("GetPicInfobytableid : " + JSON.stringify(data));
            if (data.success == true) {
                var o = data.data;
                setObj(o);

                var menuid = 'actionsheetmenu' + o.attrs.table_status;
                actionSheet(menuid);
            } else {
                WinAlert(data.message);
            }
        },
        error: function () {
            WinAlert("系统异常或者长时间未操作，请关闭系统重新登录");
        }
    });
}

///点餐
function addfood()
{
//    var url = "/System/food/index/" + $("#hidtableid").val();
//    
//    window.locaaddfoodtion.href = url;
    var formParam = {
    		tableid :  $("#hidtableid").val()
    };
    post('/System/food/enterAddFood', formParam);
}


function changesta1T0() {
    var formParam = { table_id: $("#hidtableid").val() };
    $.ajax({
        type: 'post',
        url: '/System/restindex/ChangeSta1T0',
        data: formParam,
        cache: false,
        dataType: 'json',
        success: function (data) {
            if (data.success == true) {
                WinAlert("取消成功", refresh);
            }
            else {
                WinAlert(data.message);
            }
        },
        error: function () {
            WinAlert("系统异常或者长时间未操作，请关闭系统重新登录");
        }
    });
}

function refresh()
{
	//如果该方法没有规定参数，或者参数是 false，它就会用 HTTP 头 If-Modified-Since 来检测服务器上的文档是否已改变。如果文档已改变，reload() 会再次下载该文档。如果文档未改变，则该方法将从缓存中装载文档。
    window.location.reload();
}

///加餐
function addfood1() {
    var formParam = {
    		tableid :  $("#hidtableid").val()
    };
    post('/System/food/enterAddFood1', formParam);
}


///结账
function checkout() {
    var formParam = {
    		tableid :  $("#hidtableid").val()
    };
    post('/System/food/CheckOut', formParam);
    
}

///订单详情
function orderdetail() {
    var formParam = {
    		tableid :  $("#hidtableid").val()
    };
    post('/System/food/Orderdetail', formParam);
}


///确认
function confirm() {
    var formParam = {
    		tableid :  $("#hidtableid").val()
    };
    post('/System/restIndex/Confirm', formParam);
}


///确认
function addfoodconfirm() {
//    var url = "/food/AddFoodConfirm/" + $("#hidtableid").val();
//    window.location.href = url;
    
    var formParam = {
    		tableid :  $("#hidtableid").val()
    };
    post('/System/restindex/AddFoodConfirm', formParam);
}

///选择包厢或者大厅
function seltable(obj,type)
{
    $(".tabbable li").removeClass("active");
    $(obj).addClass("active");

    $(".tablepic").css("display","");

    $(".tablepic").each(function (index) {
        var o = $(this).find(".tabletype");
        if ($(o).val() != type)
        {
            $(this).css("display", "none");
        }

    })
}

function post(url, params) {
    var temp = document.createElement("form");
    temp.action = url;
    temp.method = "post";
    temp.style.display = "none";
    for (var x in params) {
        var opt = document.createElement("input");
        opt.name = x;
        opt.value = params[x];
        temp.appendChild(opt);
    }
    document.body.appendChild(temp);
    temp.submit();
    return temp;
}   
